#! /usr/bin/env python

import time

def print_timing(func):
    def wrapper(*arg):
        t1 = time.time()
        res = func(*arg)
        t2 = time.time()
        dt = t2 - t1
        if dt < 1.00:
            print '%s took %0.3f ms' % (func.func_name,dt*1000.0)
        elif dt >= 1.00  and dt < 60.0:
            print '%s took %0.3f s' % (func.func_name,dt)    
        elif dt >= 60.0 and dt <= 3600.0:
            dt = dt/60.0
            print '%s took %0.3f min' % (func.func_name,dt)
        else:
            dt = dt/(60.0*60.0)
            print '%s took %0.3f hr' % (func.func_name,dt)
        return res    
    return wrapper

class stopwatch:

    def __init__(self):
        self.t = time.time() 
    
    def stop(self):
        dt = time.time() - self.t
        if dt < 1.00:
            print 'time elapsed = %0.3f ms' % (dt*1000.0)
        elif dt >= 1.00  and dt < 60.0:
            print 'time elapsed = %0.3f s' % (dt)    
        elif dt >= 60.0 and dt <= 3600.0:
            dt = dt/60.0
            print 'time elapsed = %0.3f min' % (dt)
        else:
            dt = dt/(60.0*60.0)
            print 'time elapsed = %0.3f hr' % (dt)

            
